package com.tuniu.agents.agent;

import com.tuniu.agents.common.TuZhiApiConstants;
import org.springframework.ai.chat.client.ChatClient;

/**
 * Agent Factory Method
 */
public class AgentFactory {

    /**
     * Model Builder
     */
    private final ChatClient.Builder modelBuilder;
    /**
     * Agent Group Manager
     * Reduces the repetition of injected parameters
     */
    private final AgentManagerGroup agentManagerGroup;

    // Constructor, inject all dependencies
    public AgentFactory(AgentManagerGroup agentManagerGroup) {
        this.modelBuilder = agentManagerGroup.getModelBuilder();
        this.agentManagerGroup = agentManagerGroup;
    }

    // Create an instance of BaseAgent using the default promptTemplateId
    public Agent createOptionsAgent(AgentOptions agentOptions) {
        ChatClient.Builder builderCopy = this.modelBuilder.clone();
        return new OptionsAgent(agentManagerGroup, agentOptions);
    }

    /**
     * Create a BaseAgent
     *
     * @param agentId          agentId
     * @param promptTemplateId Prompt template ID
     * @return BaseAgent instance
     */
    public BaseAgent createBaseAgent(String agentId, String promptTemplateId) {
        return new BaseAgent(agentManagerGroup, agentId, promptTemplateId);
    }

    /**
     * Create a BaseAgent
     *
     * @param agentId             agentID
     * @param promptTemplateId    Prompt template ID
     * @param chatMemoryInRecord  Whether to record incoming chat memory
     * @param chatMemoryOutRecord Whether to record outgoing chat memory
     * @return BaseAgent instance
     */
    public BaseAgent createBaseAgent(String agentId, String promptTemplateId, boolean chatMemoryInRecord, boolean chatMemoryOutRecord) {
        AgentOptions agentOptions = new AgentOptions(agentId, promptTemplateId);
        agentOptions.getAdvisorParam().put(TuZhiApiConstants.AdvisorParamKey.CHAT_MEMORY_IN_RECORD, chatMemoryInRecord);
        agentOptions.getAdvisorParam().put(TuZhiApiConstants.AdvisorParamKey.CHAT_MEMORY_OUT_RECORD, chatMemoryOutRecord);
        return new BaseAgent(agentManagerGroup, agentId, promptTemplateId);
    }

    /**
     * Create a ToolAgent
     *
     * @param agentId          agentID
     * @param promptTemplateId Prompt template ID
     * @param tools            List of tools
     * @return ToolAgent instance
     */
    public ToolAgent createToolAgent(String agentId, String promptTemplateId, String... tools) {
        ChatClient.Builder builderCopy = this.modelBuilder.clone();
        return new ToolAgent(agentManagerGroup, agentId, promptTemplateId, tools);
    }

}
